#include "gt1151.h"
#include "string.h"
#include "stdio.h"




void CT_Delay()
{
	uint8_t count=CT_I2C_DELAY_TIME;
	while(count--);
}

void CT_IIC_Start(void)
{
	CT_SDA_OUT();
	CT_IIC_SDA(1);
	CT_IIC_SCL(1);
	CT_Delay();
 	CT_IIC_SDA(0);//START:when CLK is high,DATA change form high to low
 	CT_Delay();
	CT_IIC_SCL(0);
}

void CT_IIC_Stop(void)
{
	CT_SDA_OUT();
	CT_IIC_SCL(1);
	CT_Delay();
	CT_IIC_SDA(0);
	CT_Delay();
	CT_IIC_SDA(1);
}

uint8_t CT_IIC_Wait_Ack(void)
{
	uint8_t ucErrTime=0;
	CT_SDA_IN();
	CT_IIC_SDA(1);
	CT_IIC_SCL(1);
	CT_Delay();
	while(CT_READ_SDA)
	{
		ucErrTime++;
		if(ucErrTime>250)
		{
			CT_IIC_Stop();
			return 1;
		}
		CT_Delay();
	}
	CT_IIC_SCL(0);
	return 0;
}

void CT_IIC_Ack(void)
{
	CT_IIC_SCL(0);
	CT_SDA_OUT();
	CT_Delay();
	CT_IIC_SDA(0);
	CT_Delay();
	CT_IIC_SCL(1);
	CT_Delay();
	CT_IIC_SCL(0);
}

void CT_IIC_NAck(void)
{
	CT_IIC_SCL(0);
	CT_SDA_OUT();
	CT_Delay();
	CT_IIC_SDA(1);
	CT_Delay();
	CT_IIC_SCL(1);
	CT_Delay();
	CT_IIC_SCL(0);
}

void CT_IIC_Send_Byte(uint8_t txd)
{
    uint8_t t;
	CT_SDA_OUT();
    CT_IIC_SCL(0);
	CT_Delay();
	for(t=0;t<8;t++)
    {
        CT_IIC_SDA((txd&0x80)>>7);
        txd<<=1;
		CT_IIC_SCL(1);
		CT_Delay();
		CT_IIC_SCL(0);
		CT_Delay();
    }
}

uint8_t CT_IIC_Read_Byte(unsigned char ack)
{
	uint8_t i,receive=0;
 	CT_SDA_IN();
	CT_Delay();
	for(i=0;i<8;i++ )
	{
		CT_IIC_SCL(0);
		CT_Delay();
		CT_IIC_SCL(1);
		receive<<=1;
		if(CT_READ_SDA)receive++;
	}
	if (!ack)CT_IIC_NAck();
	else CT_IIC_Ack();
 	return receive;
}










const uint8_t GT1151_CFG_TBL_BT[]=
{
//	0x82,0x40,0x01,0xE0,0x01,0x05,0x3D,0x14,0x00,0x00,0x02,0x0C,0x5F,0x4B,0x35,0x01,
//	0x00,0x06,0x06,0x1E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x38,0x00,0x00,0x64,0x08,
//	0x32,0x28,0x28,0x64,0x00,0x00,0x87,0xA0,0xCD,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//	0x00,0x00,0x00,0x00,0x00,0x00,0x8A,0x00,0x0F,0x74,0x76,0xEB,0x06,0x00,0x1E,0x52,
//	0x24,0xFA,0x1E,0x18,0x32,0xAA,0x63,0x99,0x6E,0x82,0x79,0x80,0x85,0x00,0x00,0x00,
//	0x00,0x64,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x58,0x9C,0xC0,0x94,0x85,0x28,0x0A,0x03,
//	0xAA,0x63,0x99,0x6E,0x8A,0x79,0x80,0x85,0x77,0x90,0x71,0x00,0x00,0x00,0x00,0x00,
//	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//	0x00,0x0E,0x0F,0x10,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1D,0x1F,
//	0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
//	0xFF,0x06,0x08,0x0C,0x12,0x13,0x14,0x15,0x17,0x18,0x19,0xFF,0xFF,0xFF,0xFF,0xFF,
//	0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
//	0xFF,0x00,0xC4,0x09,0x23,0x23,0x50,0x5D,0x54,0x50,0x3C,0x14,0x32,0xFF,0xFF,0x06,
//	0x51,0x00,0x8A,0x02,0x40,0x00,0xAA,0x00,0x22,0x22,0x00,0x40,0x59,0x74,0x01
//	0x83,0xE0,0x01,0x20,0x03,0x05,0x3D,0x14,0x00,0x00,0x02,0x0C,0x5F,0x4B,0x35,0x01,
//	0x00,0x06,0x06,0x1E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x38,0x00,0x00,0x64,0x08,
//	0x32,0x28,0x28,0x64,0x00,0x00,0x87,0xA0,0xCD,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//	0x00,0x00,0x00,0x00,0x00,0x00,0x8A,0x00,0x10,0x74,0x76,0xEB,0x06,0x02,0x1E,0x52,
//	0x24,0xFA,0x1E,0x18,0x32,0xAA,0x63,0x99,0x6E,0x82,0x79,0x80,0x85,0x00,0x00,0x00,
//	0x00,0x64,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x58,0x9C,0xC0,0x94,0x85,0x28,0x0A,0x03,
//	0xAA,0x63,0x99,0x6E,0x8A,0x79,0x80,0x85,0x77,0x90,0x71,0x00,0x00,0x00,0x00,0x00,
//	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
//	0x00,0x0D,0x0E,0x0F,0x10,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1D,
//	0x1F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
//	0xFF,0x06,0x08,0x0C,0x12,0x13,0x14,0x15,0x17,0x18,0x19,0xFF,0xFF,0xFF,0xFF,0xFF,
//	0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
//	0xFF,0x00,0xC4,0x09,0x23,0x23,0x50,0x5D,0x54,0x50,0x3C,0x14,0x32,0xFF,0xFF,0x06,
//	0x51,0x00,0x8A,0x02,0x40,0x00,0xAA,0x00,0x22,0x22,0x00,0x40,0x3E,0x9B,0x01
	0x83,0xE0,0x01,0x20,0x03,0x05,0x3D,0x14,0x00,0x00,0x02,0x0C,0x5F,0x4B,0x35,0x01,
	0x00,0x06,0x06,0x1E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x38,0x00,0x00,0x64,0x08,
	0x32,0x28,0x28,0x64,0x00,0x00,0x87,0xA0,0xCD,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x8A,0x00,0x10,0x74,0x76,0xEB,0x06,0x02,0x1E,0x52,
	0x24,0xFA,0x1E,0x18,0x32,0xAA,0x63,0x99,0x6E,0x82,0x79,0x80,0x85,0x00,0x00,0x00,
	0x00,0x64,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x58,0x9C,0xC0,0x94,0x85,0x28,0x0A,0x03,
	0xAA,0x63,0x99,0x6E,0x8A,0x79,0x80,0x85,0x77,0x90,0x71,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x0D,0x0E,0x0F,0x10,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,0x1B,0x1D,
	0x1F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
	0xFF,0x06,0x08,0x0C,0x12,0x13,0x14,0x15,0x17,0x18,0x19,0xFF,0xFF,0xFF,0xFF,0xFF,
	0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
	0xFF,0x00,0xC4,0x09,0x23,0x23,0x50,0x5D,0x54,0x50,0x3C,0x14,0x32,0xFF,0xFF,0x06,
	0x51,0x00,0x8A,0x02,0x40,0x00,0xAA,0x00,0x22,0x22,0x00,0x40,0x3E,0x9B,0x01

};

void Checksum_BT(void)

{
	uint16_t checksum=0;
	uint8_t i=0;
	for(i=0;i<(sizeof(GT1151_CFG_TBL_BT)-3);i+=2)
	checksum +=((GT1151_CFG_TBL_BT[i]<<8)|GT1151_CFG_TBL_BT[i+1]);
	checksum =(~checksum)+1;
	printf("Chksum:0x%X\r\n",checksum);
}

void GT1151_RD_Reg(uint16_t reg,uint8_t *buf,uint8_t len)
{
	uint8_t i;
 	CT_IIC_Start();
 	CT_IIC_Send_Byte(GT_CMD_WR);
	CT_IIC_Wait_Ack();
 	CT_IIC_Send_Byte(reg>>8);
	CT_IIC_Wait_Ack();
 	CT_IIC_Send_Byte(reg&0XFF);
	CT_IIC_Wait_Ack();
 	CT_IIC_Start();
	CT_IIC_Send_Byte(GT_CMD_RD);
	CT_IIC_Wait_Ack();
	for(i=0;i<len;i++)
	{
    	buf[i]=CT_IIC_Read_Byte(i==(len-1)?0:1);
	}
    CT_IIC_Stop();
}


uint8_t GT1151_WR_Reg(uint16_t reg,uint8_t *buf,uint8_t len)
{
	uint8_t i;
	uint8_t ret=0;
	CT_IIC_Start();
 	CT_IIC_Send_Byte(GT_CMD_WR);
	CT_IIC_Wait_Ack();
	CT_IIC_Send_Byte(reg>>8);
	CT_IIC_Wait_Ack();
	CT_IIC_Send_Byte(reg&0XFF);
	CT_IIC_Wait_Ack();
	for(i=0;i<len;i++)
	{
    	CT_IIC_Send_Byte(buf[i]);
		ret=CT_IIC_Wait_Ack();
		if(ret)break;
	}
    CT_IIC_Stop();
	return ret;
}


uint8_t GT1151_Send_Cfg_BT(uint8_t mode)
{
	uint16_t checksum=0;
	uint8_t buf[3];
	uint8_t i=0;
	for(i=0;i<(sizeof(GT1151_CFG_TBL_BT)-3);i+=2)
	checksum +=((GT1151_CFG_TBL_BT[i]<<8)|GT1151_CFG_TBL_BT[i+1]);
	checksum =(~checksum)+1;

	buf[0]= checksum>>8;
	buf[1]= checksum;
	buf[2]= mode;

	GT1151_WR_Reg(GT_CFGS_REG,(uint8_t*)GT1151_CFG_TBL_BT,sizeof(GT1151_CFG_TBL_BT)-3);
	GT1151_WR_Reg(GT_CHECK_REG,buf,3);
	return 0;
}


StructTouchDev TOUCH;

uint8_t Cfg_Info[250];

uint8_t GT1151_init()
{
	uint8_t temp[5]={0};

	GT_RST(0);
	HAL_Delay(50);
	GT_RST(1);
	HAL_Delay(200);

//	Checksum_BT();

	GT1151_RD_Reg(GT1151Q_PID_REG,temp,4);
	printf("CTP ID:GT%s\r\n",temp);


	if(strcmp((char*)temp,"1158")==0)
	{

		GT1151_Send_Cfg_BT(1);
		HAL_Delay(100);

//		GT1151_RD_Reg(0x8050,Cfg_Info,239);
//		printf("Config Info:\r\n");
//		for(int i = 0; i < 239; i++ )
//		{
//			printf("0x%02x,",Cfg_Info[i]);
//			if((i+1)%10==0)
//			printf("\r\n");
//		}
//		printf("\n");

		return 0;
	}
	else
	{
		printf("touch error\r\n");
		return 1;
	}
}


uint16_t GT1151_REG_ADDR[5] = {GT1151Q_TP1_REG, GT1151Q_TP2_REG, GT1151Q_TP3_REG, GT1151Q_TP4_REG, GT1151Q_TP5_REG};//各个触点数据地址

uint8_t GT1151_Scan()
{

  uint8_t  regValue;
  uint8_t  buf[6];
  uint8_t  i;
  uint8_t  reval=0;
//  uint16_t swap;

  //读取状态寄存器
  GT1151_RD_Reg(GT1151Q_GSTID_REG, &regValue, 1);//读取地址为GT1151Q_GSTID_REG，长度为1字节的数据至regValue中
//  printf("GT1151Q_GSTID_REG->0x%02x\r\n",regValue);

  TOUCH.pointNum  = regValue & 0x0F;

  //清除状态寄存器
  regValue = 0;
  GT1151_WR_Reg(GT1151Q_GSTID_REG, &regValue, 1);

  if(TOUCH.pointNum<=5)
  {
	  //循环获取5个触摸点数据
	  for(i = 0; i < 5; i++)
	  {
	    //检测到触点
	    if(TOUCH.pointNum >= (i + 1))
	    {
	      GT1151_RD_Reg(GT1151_REG_ADDR[i], buf, 6);
	      TOUCH.point[i].x    = (buf[1] << 8) | buf[0];
	      TOUCH.point[i].y    = (buf[3] << 8) | buf[2];
	      TOUCH.point[i].size = (buf[5] << 8) | buf[4];

//	      //横屏坐标转换
//	      if(1 == s_structLCDDev.dir)
//	      {
//	        swap            = TOUCH->point[i].x;
//	        TOUCH->point[i].x = TOUCH->point[i].y;
//	        TOUCH->point[i].y = swap;
//	        TOUCH->point[i].x = 800 - TOUCH->point[i].x;
//	      }

	      //标记触摸点按下
	      TOUCH.pointFlag[i] = 1;
	      reval=1;
//	      printf("x->%d , y->%d\r\n",TOUCH.point[0].x,TOUCH.point[0].y);
	    }
	    //未检测到触点
	    else
	    {
	      TOUCH.pointFlag[i] = 0;       //未检测到
	      TOUCH.point[i].x   = 0xFFFF;  //无效值
	      TOUCH.point[i].y   = 0xFFFF;  //无效值
	    }
	  }
  }

  return reval;
}



















